package com.cy.module;

import com.cy.ext.plugin.route.ControllerBind;
import com.cy.model.LicenseTokenLog;
import com.jfinal.plugin.activerecord.Page;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

@ControllerBind(controllerKey = "/clmp/licenseTokenLog")
public class LicenseTokenLogController extends WebApiController {

    @Override
    public void index() {
        LicenseTokenLog licenseTokenLog = getModel(LicenseTokenLog.class);
        int page = getParaToInt("pageIndex");
        int pageSize = getParaToInt("limit");
        Map<String, String[]> parasMap = getParaMap();
        if (null != parasMap){
            String start = null ;
            if(null != parasMap.get("start_time")){
                start = parasMap.get("start_time")[0];
            }
            String end = null;
            if (null != parasMap.get("end_time")){
                end = parasMap.get("end_time")[0];
            }
            if (StringUtils.isNotBlank(start)){
                licenseTokenLog.put("start",start);
            }
            if (StringUtils.isNotBlank(end)){
                licenseTokenLog.put("end",end);
            }
        }
        Page<LicenseTokenLog> licenseTokenLogPage = LicenseTokenLog.dao.paginate(page, pageSize, LicenseTokenLog.SELECT_ALL, licenseTokenLog);
        renderBuiPage(licenseTokenLogPage, 200);
    }

    @Override
    public void update() {
        int id = getParaToInt();
        LicenseTokenLog tempLicenseTokenLog = getModel(LicenseTokenLog.class);
        LicenseTokenLog licenseTokenLog = LicenseTokenLog.dao.findById(id);
        licenseTokenLog.cover(tempLicenseTokenLog);

        if (licenseTokenLog.update()) {
            renderSuccess();
        } else {
            renderFail();
        }
    }

    @Override
    public void save() {
        LicenseTokenLog licenseTokenLog = getModel(LicenseTokenLog.class);
        if (licenseTokenLog.save()) {
            renderSuccess();
        } else {
            renderFail();
        }
    }

    @Override
    public void delete() {
        String[] ids = getPara().split("-");
        for (String id : ids) {
            LicenseTokenLog.dao.deleteById(id);
        }
        renderSuccess();
    }
}
